package de.koller.worlders.engine.entity.enc.gu;

import de.koller.oceanFramework.math.Vec;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.tree.Mother;
import de.koller.worlders.engine.entity.AABBObject;
import de.koller.worlders.engine.entity.enc.EncRoot;
import de.koller.worlders.engine.entity.enc.pathfinding.IGUMover;
import de.koller.worlders.render.IGLRender;
import de.koller.worlders.render.VisualMap;
import de.koller.worlders.render.visual.DisplayRenderObject;

public class GUMob extends AABBObject implements IGLRender, IGUMover {
	
	@Mother EncRoot eroot;
	@Child private final TargetV playerTargeting = new TargetV();
	@Child public final GUMovement movement = new GUMovement();
	private boolean playerOver = false;
	
	public GUMob( int x, int y ) {
		super( x+0.2f, y+0.2f, 0.6f, 0.6f );
	}

	@Override
	protected void init() {
		super.init();
		
		this.setVisual( VisualMap.guMob );
		wroot.rendering.addWorldElement( this );
	}

	@Override
	protected void release() {
		super.release();
		
		wroot.rendering.removeWorldlement( this );
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		if( !eroot.isEnabled() )
			return;
		
		playerOver = inBounds( eroot.wroot.getEntityContainer().getMouseWorldPosition() );
		if( !playerOver ) {
			if( wroot.getInput().isMousePressed( 0 ) && eroot.isTargetedMob( this ) )
				eroot.setTargetedMob( null );
			return;
		}
		
		if( wroot.getInput().isMousePressed( 0 ) )
			eroot.setTargetedMob( this );
	}
	
	@Override
	public void renderGL( int elapsedTime ) {
		DisplayRenderObject.renderAABB( this, 1.01f );
		
		if( !eroot.isEnabled() )
			return;
		
		if( eroot.isTargetedMob( this ) )
			playerTargeting.renderTarget( this.getPosition().getXf()+.3f, this.getPosition().getYf()+.3f, 1f, elapsedTime );
		else if( playerOver )
			playerTargeting.renderOver( this.getPosition().getXf()+.3f, this.getPosition().getYf()+.3f, 1f, elapsedTime );
	}
	
	private boolean inBounds( Vec p ) {
		if( p.getX() > this.getPosition().getX() + this.getSize().getX() )
			return false;
		if( p.getX() < this.getPosition().getX() )
			return false;
		if( p.getY() > this.getPosition().getY() + this.getSize().getY() )
			return false;
		if( p.getY() < this.getPosition().getY() )
			return false;
		
		return true;
	}

	public boolean isPlayerOver() {
		return playerOver;
	}
	
}
